<template>
  <div v-if="$special">
    <el-button type="success" @click="visible = true" class="el-icon-download">导出特殊账号订单</el-button>
    <el-dialog
        title="导出特殊账号订单"
        width="600px"
        @close="cancel"
        :close-on-click-modal="false"
        :visible.sync="visible">
      <el-form :model="form" :rules="rules" label-width="100px" ref="Form">
        <el-form-item label="日期:" prop="beginTime">
          <date-time-picker :start.sync="form.beginTime" :end.sync="form.endTime"/>
        </el-form-item>
        <el-form-item label="状态:">
          <span>揽收</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <submit-button ref="Submit" text="导 出" @submit="submit"/>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {exportSpecialExpressOrderApi} from '@/api/export';
  import {resetForm} from "@/utils/common";
  import DateTimePicker from '@/components/DateTimePicker'

  export default {
    name: 'ExportSpecial',
    components: {DateTimePicker},
    data() {
      return {
        isLoading: false,
        visible: false,
        form: {
          beginTime: '',
          endTime: '',
          state: 0
        },
        rules: {
          beginTime: {required: true, message: '请选择日期', trigger: 'change'}
        }
      }
    },
    methods: {
      submit() {
        this.$refs['Form'].validate(valid => {
          if (!valid) return false;
          this.$refs.Submit.start()
          let param = {...this.form}
          exportSpecialExpressOrderApi(param)
            .then(result => {
              this.$refs.Submit.stop()
              const blob = new Blob([result]);
              this.$fileSaver.saveAs(blob, "特殊账号订单数据.xlsx");
              this.cancel();
            })
            .catch(err => {
              this.$refs.Submit.stop()
              err.response.data.text().then(res => {
                this.$errorMsg(JSON.parse(res).message)
              })
            })
        });
      },
      cancel() {
        resetForm(this)
      }
    }
  }
</script>
